#include "Reactor.hpp"
#include "Sock.hpp"
#include "Accepter.hpp"
#include "Util.hpp"

void Usage(std::string proc)
{
    std::cout << "Usage: " << proc << "port" << std::endl;
}

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        Usage(argv[0]);
        exit(1);
    }
    uint16_t port = atoi(argv[1]);
    // 1、创建套接字,监听
    int listen_sock = Sock::Socket();
    SetNonBlock(listen_sock); //将套接字设置成非阻塞状态
    Sock::Bind(listen_sock, port);
    Sock::Listen(listen_sock);

    // 2、创建Reactor对象
    Reactor *R = new Reactor();
    R->InitReactor(); //初始化

    // 3、给Reactor反应堆中加数据
    // 3.1 创建数据
    Event *evp = new Event();
    evp->sock = listen_sock;
    evp->R = R;

    // Accepter:链接管理器
    evp->RegisterCallback(Accepter, nullptr, nullptr);
    // 3.2将数据放入Reactor
    R->InsertEvent(evp, EPOLLIN | EPOLLET);

    // 4、开始进行事件派发
    int timeout = 1000; //单位ms
    for (;;)
    {
        R->Dispatcher(timeout);
    }
}